﻿using System.IO;
using System.Text;
using LFSRecord2.Model;

namespace LFSRecord2.Misc
{
    public static class Logger
    {
        public static void LogError(string error)
        {
            error += "\r\n";

            using (FileStream fs = File.OpenWrite(LfsRecordController.StartupDirectory + "\\err.log"))
            {
                fs.Position = fs.Length;
                fs.Write(Encoding.ASCII.GetBytes(error), 0, error.Length);
            }
        }

        public static void LogDebug(string msg, bool includeHex)
        {
            if (includeHex)
                msg += "   " + DumpUnicodeHex(msg);
            msg += "\r\n";

            using (FileStream fs = File.OpenWrite(LfsRecordController.StartupDirectory + "\\deb.log"))
            {
                fs.Position = fs.Length;
                fs.Write(Encoding.ASCII.GetBytes(msg), 0, msg.Length);
            }
        }

        public static void LogDebug(string msg)
        {
            LogDebug(msg, false);
        }

        public static string DumpUnicodeHex(string msg)
        {
            int len = msg.Length;
            char[] unicodeChars = new char[len];
            int[] unicodeInts = new int[len];
            string hexOut = "";

            for (int a = 0; a < len; a++)
            {
                unicodeChars[a] = msg[a];
                unicodeInts[a] = (int)msg[a];
                hexOut += unicodeInts[a] + " ";
            }

            return hexOut;
        }
    }
}
